import java.util.ArrayList;
import java.util.List;

public class Solution1260 {
    public List<List<Integer>> shiftGrid(int[][] grid, int k) {
        int[][] f=new int[grid.length][grid[0].length];
        for (int i=0;i<grid.length;i++){
            for (int j=0;j<grid[0].length;j++){
                int x=(i*grid[0].length+j+k)%(grid.length*grid[0].length);
                f[x/grid[0].length][x%grid[0].length]=grid[i][j];
            }
        }
        List<List<Integer>> ans=new ArrayList<>();
        for (int[] ints : f) {
            ans.add(new ArrayList<>());
            for (int j = 0; j < f[0].length; j++) {
                ans.get(ans.size() - 1).add(ints[j]);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1260().shiftGrid(new int[][]{{1,2,3},{4,5,6},{7,8,9}},1));
    }
}
